﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;

public partial class Departments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
                if (service == null)
                {
                    service = new HeadquarterWSClient();
                }

                List<departmentdescription> departments = new List<departmentdescription>();
                departments.AddRange(service.getAllDepartments().ToList());
                if (departments.Count == 0)
                {
                    departments.Add(new departmentdescription() { id = 0, name = "", url = "" });
                    gvDepartments.DataSource = departments;
                    gvDepartments.DataBind();

                    int columnsCount = gvDepartments.Columns.Count;
                    gvDepartments.Rows[0].Cells.Clear();// clear all the cells in the row
                    gvDepartments.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                    gvDepartments.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
                    gvDepartments.Rows[0].Cells[0].Height = 0;
                    gvDepartments.Rows[0].Cells[0].Style["padding"] = "0";
                }
                else
                {
                    gvDepartments.DataSource = service.getAllDepartments();
                    gvDepartments.DataBind();
                }
            }
            catch (System.Exception ex)
            {
                if ((Application["ThrowExceptions"] as bool?) == true)
                    throw ex;
            }
        }
    }

    protected void gvDepartments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.PageIndex = e.NewPageIndex;
            gvDepartments.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void DeleteDepartment(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            Button btnDelete = (Button)sender;
            service.removeDepartment(int.Parse(btnDelete.CommandArgument));

            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.DataBind();

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void AddNewDepartment(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            //string DeptID = ((TextBox)gvDepartments.FooterRow.FindControl("lblDepartmentID")).Text;
            string Name = ((TextBox)gvDepartments.FooterRow.FindControl("txtName")).Text;
            string URL = ((TextBox)gvDepartments.FooterRow.FindControl("txtUrl")).Text;

            service.addNewDepartment(Name, URL);

            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.DataBind();
            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvDepartments_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.EditIndex = e.NewEditIndex;
            gvDepartments.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvDepartments_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvDepartments.EditIndex = -1;
            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvDepartments_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            string DeptID = ((Label)gvDepartments.Rows[e.RowIndex].FindControl("lblDepartmentID")).Text;
            string Name = ((TextBox)gvDepartments.Rows[e.RowIndex].FindControl("txtName")).Text;
            string URL = ((TextBox)gvDepartments.Rows[e.RowIndex].FindControl("txtUrl")).Text;

            ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "alertNotImplemented", "alert('Update operation not fully implemented! No changes were made to departments.');", true);

            gvDepartments.DataSource = service.getAllDepartments();
            gvDepartments.EditIndex = -1;
            gvDepartments.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }
}